package top.dfghhj.anhanx.oauth.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.dfghhj.anhanx.oauth.dao.SysMenuDao;
import top.dfghhj.anhanx.oauth.dao.SysRoleMenuDao;
import top.dfghhj.anhanx.oauth.entity.DO.SysMenu;
import top.dfghhj.anhanx.oauth.entity.DO.SysRoleMenu;
import top.dfghhj.anhanx.oauth.service.SysMenuService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuDao sysMenuDao;

    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public List<SysMenu> findByRoleId(Long roleId) {
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuDao.findByRoleId(roleId);
        if (CollectionUtils.isEmpty(sysRoleMenuList)) {
            return null;
        }
        List<SysMenu> sysMenuList = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
            SysMenu sysMenu = sysMenuDao.findById(sysRoleMenu.getMenuId());
            if (sysMenu != null) {
                sysMenuList.add(sysMenu);
            }
        }
        return sysMenuList;
    }
}
